package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public int add(ProductModel productModel) {
        String sql = "insert into product (name,normal_price,price,content,is_show,is_recommend,img_href,cuisine_id) " +
                "values('" + productModel.getName() + "'," +
                "" + productModel.getNormalPrice() + "," +
                "" + productModel.getPrice() + "," +
                "'" + productModel.getContent() + "'," +
                "" + productModel.getIsShow() + "," +
                "" + productModel.getIsRecommend() + "," +
                "'" + productModel.getImgHref() + "'," +
                "" + productModel.getCuisineId() + ")";
        return JDBC.excuteUpdate(sql);
    }

    @Override
    public List<ProductModel> findAll(ProductModel productModel) {
        String sql = "select p.*,c.name as cuisine_name from product p,cuisine c where p.cuisine_id=c.id";
        sql += " and 1=1     ";
        if (!"".equals(productModel.getName())) {
            sql += " and p.name like '%" + productModel.getName() + "%'";
        }
//        if (!"".equals(productModel.getContent())) {
//            sql += " and   ";
//        }
        sql += " order by id desc ";
        sql += " limit " + (productModel.getPageModel().getCurPage() - 1) * productModel.getPageModel().getPageSize() + "," + productModel.getPageModel().getPageSize();
        ResultSet resultSet = JDBC.getResultSet(sql);
        return getModel(resultSet);
    }

    @Override
    public ProductModel findById(Integer id) {
        String sql = "select p.*,c.name as cuisine_name from product p,cuisine c where p.cuisine_id=c.id and p.id=" + id;
        ResultSet resultSet = JDBC.getResultSet(sql);
        return getModel(resultSet).get(0);
    }

    @Override
    public int getCount(ProductModel productModel) {
        String sql = "select count(*) as count from product p,cuisine c where p.cuisine_id=c.id";
        sql += " and 1=1     ";
        if (!"".equals(productModel.getName())) {
            sql += " and p.name like '%" + productModel.getName() + "%'";
        }
//        if (!"".equals(productModel.getContent())) {
//            sql += " and   ";
//        }
        ResultSet resultSet = JDBC.getResultSet(sql);
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    private List<ProductModel> getModel(ResultSet resultSet) {
        List<ProductModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                ProductModel productModel = new ProductModel();
                productModel.setId(resultSet.getInt("id"));
                productModel.setName(resultSet.getString("name"));
                productModel.setCuisineName(resultSet.getString("cuisine_name"));
                productModel.setNormalPrice(resultSet.getDouble("normal_price"));
                productModel.setPrice(resultSet.getDouble("price"));
                productModel.setContent(resultSet.getString("content"));
                productModel.setIsShow(resultSet.getInt("is_show"));
                productModel.setIsRecommend(resultSet.getInt("is_recommend"));
                productModel.setImgHref(resultSet.getString("img_href"));
                productModel.setCuisineId(resultSet.getInt("cuisine_id"));
                list.add(productModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
